conv.crb(Ver.0.4)

  1: out_fp = fopen("../builtin.c", "w");
  2: 
  3: dump("head.txt", out_fp);
  4: 
  5: prefixes = {};
  6: for (i = 0; i < ARGS.size(); i++) {
  7:     suffix = ARGS[i].substr(ARGS[i].length()-4, 4);
  8:     if (suffix != ".crb") {
  9:         fputs("bad suffix." + suffix + "\n", STDERR);
 10:         exit(1);
 11:     }
 12:     prefix = ARGS[i].substr(0, ARGS[i].length()-4);
 13:     prefixes.add(prefix);
 14: 
 15:     fp = fopen(ARGS[i], "r");
 16:     fputs("static char *st_" + prefix + "_src[] = {\n", out_fp);
 17:     do_conv(fp, out_fp);
 18:     fputs("NULL};\n\n", out_fp);
 19: 
 20:     fclose(fp);
 21: }
 22: 
 23: for (i = 0; i < prefixes.size(); i++) {
 24:     fputs("static char **st_src_array[] = {\n", out_fp);
 25:     fputs("    st_" + prefixes[i] + "_src,\n", out_fp);
 26:     fputs("};\n\n", out_fp);
 27: }
 28: 
 29: dump("tail.txt", out_fp);
 30: 
 31: function dump(filename, out_fp) {
 32:     fp = fopen(filename, "r");
 33: 
 34:     while ((str = fgets(fp)) != null) {
 35:         fputs(str, out_fp);
 36:     }
 37:     fclose(fp);
 38: }
 39: 
 40: function do_conv(in_fp, out_fp) {
 41: 
 42:     while ((str = fgets(in_fp)) != null) {
 43:         fputs("    \"", out_fp);
 44:         for (i = 0; i < str.length(); i++) {
 45:             ch = str.substr(i, 1);
 46:             if (ch == "\"") {
 47:                 fputs("\\\"", out_fp);
 48:             } elsif (ch == "\\") {
 49:                 fputs("\\\\", out_fp);
 50:             } elsif (ch == "\t") {
 51:                 fputs("\\t", out_fp);
 52:             } elsif (ch == "\n") {
 53:                 fputs("\\n", out_fp);
 54:             } else {
 55:                 fputs(ch, out_fp);
 56:             }
 57:         }
 58:         fputs("\",\n", out_fp);
 59:     }
 60: }
戻る